package pers.dzk.fivelnarow.player.computer;

import pers.dzk.fivelnarow.config.CheckerboardConfig;
import pers.dzk.fivelnarow.piece.PieceType;

import java.awt.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 10843
 * 评估类
 */
public class EvaluateSystem {
    private static EvaluateSystem evaluateSystem;
    private TestPiece[][] pieces;
    private PieceType friend;
    private PieceType enemy;
    private int[][] positionsScore;
    private Point[] clearList;
    private int clearListIndex;
    private EvaluateSystem(){
        type = new HashMap();
        //0:空棋  1:己方棋   2:障碍物(敌方棋子和墙)
        //活:左右两边畅通无堵    死:左或右隔壁被堵   虚:隔空堵路  跳:非连续性
        //所有死棋型新增判断卡墙角
        //活1
        type.put("000010000",80);
        //虚1
        type.put("0000102",40);
        //死1     
        type.put("000012",20);
        //活2
        type.put("00011000",8000);
        //活跳2
        type.put("0010100",6000);
        //虚2
        type.put("0001102",4000);
        //死2
        type.put("000112",2000);
        //死跳2
        type.put("210100",1500);
        //活3
        type.put("0011100",800000);
        //活跳3
        type.put("011010",600000);
        //虚3
        type.put("0011102",400000);
        //死3
        type.put("001112",200000);
        //死跳3-1
        type.put("211010",150000);
        //死跳3-2
        type.put("011012",150000);
        //活4
        type.put("011110",80000000);
        //活跳4-1
        type.put("011101",60000000);
        //活跳4-2
        type.put("11011",60000000);
        //死4
        type.put("011112",20000000);
        //死跳4
        type.put("211101",15000000);
        //5连
        type.put("11111",Integer.MAX_VALUE);
        int l = CheckerboardConfig.getLineCount(),let = 0,rig = l - 1,y = 0;
        positionsScore = new int[l][l];
        while (let <= rig) {
            while (let <= rig) {
                positionsScore[y][let] = y;
                positionsScore[y][rig] = y;
                positionsScore[l - 1 - y][let] = y;
                positionsScore[l - 1 - y][rig] = y;
                positionsScore[let][y] = y;
                positionsScore[rig][y] = y;
                positionsScore[let++][l-1-y] = y;
                positionsScore[rig--][l-1-y] = y;
            }
            let = ++y;
            rig = l - 1 - y;
        }
        pieces = new TestPiece[l][l];
        for (int i = 0; i < l; i++){
            for (int j = 0; j < l; j++) {
                pieces[i][j] = new TestPiece();
                pieces[i][j].index = new Point(j,i);
                pieces[i][j].code = PieceType.空;
            }
        }
        clearList = new Point[l*l];
    }
    public void update(int x, int y, PieceType pieceType) {
        pieces[y][x].code = pieceType;
    }
    public TestPiece[][] select(){
        return pieces;
    }
    public static EvaluateSystem getEvaluate() {
        if (evaluateSystem == null) {
           evaluateSystem = new EvaluateSystem();
        }
        return evaluateSystem;
    }
    private Map<String,Integer> type;
    public int getScore(PieceType pieceType) {
        friend = pieceType;
        enemy = friend == PieceType.黑棋 ? PieceType.白棋 : PieceType.黑棋;
        int computerScore = getPlayerScore(friend);
        int peopleScore = getPlayerScore(enemy);
        clear();
        if(computerScore == type.get("11111")){
            return computerScore;
        }
        if(peopleScore == type.get("11111")){
            return -peopleScore;
        }
        return computerScore - peopleScore;
    }
    private void clear(){
        for (int i = 0; i < clearListIndex; i++) {
            Point p = clearList[i];
            pieces[p.y][p.x].clear();
        }
        clearListIndex = 0;
    }
    private int getPlayerScore(PieceType friend){
        //实际分数为：我方局势分剪掉敌方局势分
        int score = 0;
        PieceType enemy = friend == PieceType.黑棋 ? PieceType.白棋 : PieceType.黑棋;
        for (int y = 0; y < pieces.length; y++) {
            for (int x = 0; x < pieces.length; x++) {
                if(pieces[y][x].code == friend){
                    for (int d = 1; d <= 4; d++) {
                        TestPiece[] line = getLine(x,y,d);
                        if(isInvalid(line,d)){
                            continue;
                        }
                        if(score < 0 || score >= type.get("11111")){
                            return type.get("11111");
                        }
                        score += getTypeScore(line,friend,enemy,d);
                        score += positionsScore[y][x];
                    }
                }
            }
        }
        return score;
    }
    private int getTypeScore(TestPiece[] line, PieceType friend, PieceType enemy, int d){
        int score;
        if((score = is5(line,friend,enemy,d)) > 0){
            return score;
        }
        if((score = isLive4(line,friend,enemy,d)) > 0){
            return score;
        }
        if((score = isJump4(line,friend,enemy,d)) > 0){
            return score;
        }
        if((score = isDie4(line,friend,enemy,d)) > 0){
            return score;
        }
        if((score = isDieJump4(line,friend,enemy,d)) > 0){
            return score;
        }
        if((score = isLive3(line,friend,enemy,d)) > 0){
            return score;
        }
        if((score = isJump3(line,friend,enemy,d)) > 0){
            return score;
        }
        if((score = isFalse3(line,friend,enemy,d)) > 0){
            return score;
        }
        if((score = isDie3(line,friend,enemy,d)) > 0){
            return score;
        }
        if((score = isDieJump3(line,friend,enemy,d)) > 0){
            return score;
        }
        if((score = isLive2(line,friend,enemy,d)) > 0){
            return score;
        }
        if((score = isJump2(line,friend,enemy,d)) > 0){
            return score;
        }
        if((score = isFalse2(line,friend,enemy,d)) > 0){
            return score;
        }
        if((score = isDie2(line,friend,enemy,d)) > 0){
            return score;
        }
        if((score = isDieJump2(line,friend,enemy,d)) > 0){
            return score;
        }
        if((score = isLive1(line,friend,enemy,d)) > 0){
            return score;
        }
        if((score = isFalse1(line,friend,enemy,d)) > 0){
            return score;
        }
        if((score = isDie1(line,friend,enemy,d)) > 0){
            return score;
        }
        return 0;
    }
    private boolean isInvalid(TestPiece[] line, int d){
        for (int i = 0; i < line[4].invalidIndex;i++){
            if(line[4].invalid[i] == d){
                return true;
            }
        }
        return false;
    }

    /**
     * 获取一组棋子
     * @param x x轴
     * @param y y轴
     * @param d 方向
     * @return
     */
    private TestPiece[] getLine(int x, int y, int d) {
        TestPiece[] line = new TestPiece[9];
        //1.上下  2.左右    3.左斜    4.右斜
        //=null:防撞墙(数组越界)
        switch (d) {
            case 1:
                for (int i = 0; i < line.length; i++) {
                    if(y+4-i > -1 && y+4-i < pieces.length){
                        line[i] = pieces[y+4-i][x];
                    }else{
                        line[i] = null;
                    }
                }
                break;
            case 2:
                for (int i = 0; i < line.length; i++) {
                    if(x-4+i > -1 && x-4+i < pieces.length){
                        line[i] = pieces[y][x-4+i];
                    }else{
                        line[i] = null;
                    }
                }
                break;
            case 3:
                for (int i = 0; i < line.length; i++) {
                    if(x-4+i > -1 && x-4+i < pieces.length && y-4+i > -1 && y-4+i < pieces.length){
                        line[i] = pieces[y-4+i][x-4+i];
                    }else{
                        line[i] = null;
                    }
                }
                break;
            case 4:
                for (int i = 0; i < line.length; i++) {
                    if(x+4-i > -1 && x+4-i < pieces.length && y-4+i > -1 && y-4+i < pieces.length){
                        line[i] = pieces[y-4+i][x+4-i];
                    }else{
                        line[i] = null;
                    }
                }
                break;
        }
        return line;
    }

    private void addInvalid(Point index,int direction){
        pieces[index.y][index.x].addInvalid(direction);
        clearList[clearListIndex++] = index;
    }
    private int isLive1(TestPiece[] line, PieceType friend, PieceType enemy, int direction){
        if(line[0] != null && line[8] != null){
            if(line[0].code == PieceType.空 && line[1].code == PieceType.空 &&
                    line[2].code == PieceType.空 && line[3].code == PieceType.空 &&
            line[4].code == friend && line[5].code == PieceType.空 && line[6].code == PieceType.空 &&
                    line[7].code == PieceType.空 && line[8].code == PieceType.空 ){
                return type.get("000010000");
            }
        }
        return 0;
    }
    private int isFalse1(TestPiece[] line, PieceType friend, PieceType enemy, int direction){
        //0000102##
        if(line[0] != null && line[6] != null){
            if(line[0].code == PieceType.空 && line[1].code == PieceType.空 && line[2].code == PieceType.空 &&
                    line[3].code == PieceType.空 && line[4].code == friend && line[5].code == PieceType.空 && line[6].code == enemy){
                return type.get("0000102");
            }
        }
        //##2010000
        if(line[2] != null && line[8] != null){
            if(line[2].code == enemy && line[3].code == PieceType.空 && line[4].code == friend && line[5].code == PieceType.空 &&
                    line[6].code == PieceType.空 && line[7].code == PieceType.空 && line[8].code == PieceType.空){
                return type.get("0000102");
            }
        }
        //000010###
        if(line[0] != null && line[5] != null){
            if(line[0].code == PieceType.空 && line[1].code == PieceType.空 && line[2].code == PieceType.空 &&
                    line[3].code == PieceType.空 && line[4].code == friend && line[5].code == PieceType.空 && line[6]== null){
                return type.get("0000102");
            }
        }
        //###010000
        if(line[3] != null && line[8] != null){
            if(line[2]== null && line[3].code == PieceType.空 && line[4].code == friend && line[5].code == PieceType.空 &&
                    line[6].code == PieceType.空 && line[7].code == PieceType.空 && line[8].code == PieceType.空){
                return type.get("0000102");
            }
        }
        return 0;
    }
    private int isDie1(TestPiece[] line, PieceType friend, PieceType enemy, int direction){
        //000012###
        if(line[0] != null && line[5] != null){
            if(line[0].code == PieceType.空 && line[1].code == PieceType.空 && line[2].code == PieceType.空 &&
                    line[3].code == PieceType.空 && line[4].code == friend && line[5].code == enemy){
                return type.get("000012");
            }
        }
        //###210000
        if(line[3] != null && line[8] != null){
            if(line[3].code == enemy && line[4].code == friend && line[5].code == PieceType.空 &&
                    line[6].code == PieceType.空 && line[7].code == PieceType.空 && line[8].code == PieceType.空){
                return type.get("000012");
            }
        }
        //00001####
        if(line[0] != null){
            if(line[0].code == PieceType.空 && line[1].code == PieceType.空 && line[2].code == PieceType.空 &&
                    line[3].code == PieceType.空 && line[4].code == friend && line[5]== null){
                return type.get("000012");
            }
        }
        //####10000
        if(line[4] != null && line[8] != null){
            if(line[3]== null && line[4].code == friend && line[5].code == PieceType.空 &&
                    line[6].code == PieceType.空 && line[7].code == PieceType.空 && line[8].code == PieceType.空){
                return type.get("000012");
            }
        }
        return 0;
    }

    private int isLive2(TestPiece[] line, PieceType friend, PieceType enemy, int direction){
        //以下方法执行如果成功都要给附近的棋子设置invalid防止重复计分
        //00011000#
        if(line[0] != null && line[7] != null){
            if(line[0].code == PieceType.空 && line[1].code == PieceType.空 && line[2].code == PieceType.空 &&
            line[3].code == friend && line[4].code == friend && line[5].code == PieceType.空 && line[6].code
                    == PieceType.空 && line[7].code == PieceType.空){
                addInvalid(line[3].index,direction);
                return type.get("00011000");
            }
            //#00011000
        }
        if(line[1] != null && line[8] != null){
            if(line[1].code == PieceType.空 && line[2].code == PieceType.空 && line[3].code == PieceType.空 &&
                    line[4].code == friend && line[5].code == friend && line[6].code == PieceType.空 &&
                    line[7].code == PieceType.空 && line[8].code == PieceType.空){
                addInvalid(line[5].index,direction);
                return type.get("00011000");
            }
        }
        return 0;
    }
    private int isJump2(TestPiece[] line, PieceType friend, PieceType enemy, int direction){
        //##0010100
        if (line[2] != null && line[8] != null) {
            if(line[2].code == PieceType.空 && line[3].code == PieceType.空 && line[4].code == friend &&
                    line[5].code == PieceType.空 && line[6].code == friend && line[7].code == PieceType.空 &&
                    line[8].code == PieceType.空){
                addInvalid(line[6].index,direction);
                return type.get("210100");
            }
            //0010100##
        }
        if(line[0] != null && line[6] != null){
            if(line[0].code == PieceType.空 && line[1].code == PieceType.空 && line[2].code == friend &&
                    line[3].code == PieceType.空 && line[4].code == friend && line[5].code == PieceType.空 &&
                    line[6].code == PieceType.空){
                addInvalid(line[2].index,direction);
                return type.get("210100");
            }
        }
        return 0;
    }
    private int isFalse2(TestPiece[] line, PieceType friend, PieceType enemy, int direction){
        //#0001102#
        if(line[1] != null && line[7] != null){
            if(line[1].code == PieceType.空 && line[2].code == PieceType.空 && line[3].code == PieceType.空 &&
                    line[4].code == friend && line[5].code == friend && line[6].code == PieceType.空 &&
                    line[7].code == enemy){
                addInvalid(line[5].index,direction);
                return type.get("0001102");
            }
            //#2011000#
            if(line[1].code == enemy && line[2].code == PieceType.空 && line[3].code == friend &&
                    line[4].code == friend && line[5].code == PieceType.空 && line[6].code == PieceType.空 &&
                    line[7].code == PieceType.空){
                addInvalid(line[3].index,direction);
                return type.get("0001102");
            }
        }
        //#000110##
        if(line[1] != null && line[6] != null){
            if(line[1].code == PieceType.空 && line[2].code == PieceType.空 && line[3].code == PieceType.空 &&
                    line[4].code == friend && line[5].code == friend && line[6].code == PieceType.空 &&
                    line[7]== null){
                addInvalid(line[5].index,direction);
                return type.get("0001102");
            }

        }
        //##011000#
        if(line[2] != null && line[7] != null){
            if(line[1]== null && line[2].code == PieceType.空 && line[3].code == friend &&
                    line[4].code == friend && line[5].code == PieceType.空 && line[6].code == PieceType.空 &&
                    line[7].code == PieceType.空){
                addInvalid(line[3].index,direction);
                return type.get("0001102");
            }
        }
        return 0;
    }
    private int isDieJump2(TestPiece[] line, PieceType friend, PieceType enemy, int direction){
        //###210100
        if (line[3] != null && line[8] != null) {
            if(line[3].code == enemy && line[4].code == friend && line[5].code == PieceType.空 &&
                    line[6].code == friend && line[7].code == PieceType.空 && line[8].code == PieceType.空){
                addInvalid(line[6].index,direction);
                return type.get("210100");
            }
        }
        //001012###
        if(line[0] != null && line[5] != null){
            if(line[0].code == PieceType.空 && line[1].code == PieceType.空 && line[2].code == friend &&
                    line[3].code == PieceType.空 && line[4].code == friend && line[5].code == enemy){
                addInvalid(line[2].index,direction);
                return type.get("210100");
            }
        }
        //####10100
        if (line[8] != null) {
            if(line[3] == null && line[4].code == friend && line[5].code == PieceType.空 &&
                    line[6].code == friend && line[7].code == PieceType.空 && line[8].code == PieceType.空){
                addInvalid(line[6].index,direction);
                return type.get("210100");
            }
        }
        //00101####
        if(line[0] != null){
            if(line[0].code == PieceType.空 && line[1].code == PieceType.空 && line[2].code == friend &&
                    line[3].code == PieceType.空 && line[4].code == friend && line[5] == null){
                addInvalid(line[2].index,direction);
                return type.get("210100");
            }
        }
        return 0;
    }
    private int isDie2(TestPiece[] line, PieceType friend, PieceType enemy, int direction){
        //#000112##
        if(line[1] != null && line[6] != null){
            if(line[1].code == PieceType.空 && line[2].code == PieceType.空 && line[3].code == PieceType.空 &&
                    line[4].code == friend && line[5].code == friend && line[6].code == enemy){
                addInvalid(line[5].index,direction);
                return type.get("000112");
            }
        }
        //##211000#
        if(line[2] != null && line[7] != null){
            if(line[2].code == enemy && line[3].code == friend && line[4].code == friend &&
                    line[5].code == PieceType.空 && line[6].code == PieceType.空 && line[7].code == PieceType.空){
                addInvalid(line[3].index,direction);
                return type.get("000112");
            }
        }
        //#00011###
        if(line[1] != null && line[5] != null){
            if(line[1].code == PieceType.空 && line[2].code == PieceType.空 && line[3].code == PieceType.空 &&
                    line[4].code == friend && line[5].code == friend && line[6] == null){
                addInvalid(line[5].index,direction);
                return type.get("000112");
            }
        }
        //###11000#
        if(line[3] != null && line[7] != null){
            if(line[2] == null && line[3].code == friend && line[4].code == friend &&
                    line[5].code == PieceType.空 && line[6].code == PieceType.空 && line[7].code == PieceType.空){
                addInvalid(line[3].index,direction);
                return type.get("000112");
            }
        }
        return 0;
    }
    private int isLive3(TestPiece[] line, PieceType friend, PieceType enemy, int direction){
        //#0011100#
        if(line[1] != null && line[7] != null){
            if(line[1].code == PieceType.空 && line[2].code == PieceType.空 && line[3].code == friend &&
                    line[4].code == friend && line[5].code == friend && line[6].code != PieceType.空 &&
                    line[7].code == PieceType.空){
                addInvalid(line[3].index,direction);
                addInvalid(line[5].index,direction);
                return type.get("0011100");
            }
        }
        return 0;
    }
    private int isJump3(TestPiece[] line, PieceType friend, PieceType enemy, int direction){
        //##011010#
        if(line[2] != null && line[7] != null){
            if(line[2].code == PieceType.空 && line[3].code == friend && line[4].code == friend &&
                    line[5].code == PieceType.空 && line[6].code == friend && line[7].code == PieceType.空){
                addInvalid(line[3].index,direction);
                addInvalid(line[6].index,direction);
                return type.get("011010");
            }
            //#010110##
        }
        if(line[1] != null && line[6] != null){
            if(line[1].code == PieceType.空 && line[2].code == friend && line[3].code == PieceType.空 &&
                    line[4].code == friend && line[5].code == friend && line[6].code == PieceType.空){
                addInvalid(line[2].index,direction);
                addInvalid(line[5].index,direction);
                return type.get("011010");
            }
        }
        return 0;
    }
    private int isFalse3(TestPiece[] line, PieceType friend, PieceType enemy, int direction){
        //#0011102#
        if(line[1] != null && line[7] != null){
            if(line[1].code == PieceType.空 && line[2].code == PieceType.空 && line[3].code == friend &&
                    line[4].code == friend && line[5].code == friend && line[6].code == PieceType.空 &&
                    line[7].code == enemy){
                addInvalid(line[3].index,direction);
                addInvalid(line[5].index,direction);
                return type.get("0011102");
            }
            //#2011100#
            if(line[1].code == enemy && line[2].code == PieceType.空 && line[3].code == friend &&
                    line[4].code == friend && line[5].code == friend && line[6].code == PieceType.空 &&
                    line[7].code == PieceType.空){
                addInvalid(line[3].index,direction);
                addInvalid(line[5].index,direction);
                return type.get("0011102");
            }
        }
        //#001110##
        if(line[1] != null && line[6] != null){
            if(line[1].code == PieceType.空 && line[2].code == PieceType.空 && line[3].code == friend &&
                    line[4].code == friend && line[5].code == friend && line[6].code == PieceType.空 &&
                    line[7] == null){
                addInvalid(line[3].index,direction);
                addInvalid(line[5].index,direction);
                return type.get("0011102");
            }

        }
        //##011100#
        if(line[2] != null && line[7] != null){
            if(line[1] == null && line[2].code == PieceType.空 && line[3].code == friend &&
                    line[4].code == friend && line[5].code == friend && line[6].code == PieceType.空 &&
                    line[7].code == PieceType.空){
                addInvalid(line[3].index,direction);
                addInvalid(line[5].index,direction);
                return type.get("0011102");
            }
        }
        return 0;
    }
    private int isDieJump3(TestPiece[] line, PieceType friend, PieceType enemy, int direction){
        //##211010#
        if(line[2] != null && line[7] != null){
            if(line[2].code == enemy && line[3].code == friend && line[4].code == friend &&
                    line[5].code == PieceType.空 && line[6].code == friend && line[7].code == PieceType.空){
                addInvalid(line[3].index,direction);
                addInvalid(line[6].index,direction);
                return type.get("211010");
            }
        }
        //#010112##
        if(line[1] != null && line[6] != null){
            if(line[1].code == PieceType.空 && line[2].code == friend && line[3].code == PieceType.空 &&
                    line[4].code == friend && line[5].code == friend && line[6].code == enemy){
                addInvalid(line[2].index,direction);
                addInvalid(line[5].index,direction);
                return type.get("211010");
            }
        }
        //##011012#
        if(line[2] != null && line[7] != null){
            if(line[2].code == PieceType.空 && line[3].code == friend && line[4].code == friend &&
                    line[5].code == PieceType.空 && line[6].code == friend && line[7].code == enemy){
                addInvalid(line[3].index,direction);
                addInvalid(line[6].index,direction);
                return type.get("011012");
            }
        }
        //#210110##
        if(line[1] != null && line[6] != null){
            if(line[1].code == enemy && line[2].code == friend && line[3].code == PieceType.空 &&
                    line[4].code == friend && line[5].code == friend && line[6].code == PieceType.空){
                addInvalid(line[2].index,direction);
                addInvalid(line[5].index,direction);
                return type.get("011012");
            }
        }
        //###11010#
        if(line[3] != null && line[7] != null){
            if(line[2] == null && line[3].code == friend && line[4].code == friend &&
                    line[5].code == PieceType.空 && line[6].code == friend && line[7].code == PieceType.空){
                addInvalid(line[3].index,direction);
                addInvalid(line[6].index,direction);
                return type.get("211010");
            }
        }
        //#01011###
        if(line[1] != null && line[5] != null){
            if(line[1].code == PieceType.空 && line[2].code == friend && line[3].code == PieceType.空 &&
                    line[4].code == friend && line[5].code == friend && line[6] == null){
                addInvalid(line[2].index,direction);
                addInvalid(line[5].index,direction);
                return type.get("211010");
            }
        }
        //##01101##
        if(line[2] != null && line[6] != null){
            if(line[2].code == PieceType.空 && line[3].code == friend && line[4].code == friend &&
                    line[5].code == PieceType.空 && line[6].code == friend && line[7] == null){
                addInvalid(line[3].index,direction);
                addInvalid(line[6].index,direction);
                return type.get("011012");
            }
            //##10110##
            if(line[1] == null && line[2].code == friend && line[3].code == PieceType.空 &&
                    line[4].code == friend && line[5].code == friend && line[6].code == PieceType.空){
                addInvalid(line[2].index,direction);
                addInvalid(line[5].index,direction);
                return type.get("011012");
            }
        }
        return 0;
    }
    private int isDie3(TestPiece[] line, PieceType friend, PieceType enemy, int direction){
        //#001112##
        if(line[1] != null && line[6] != null){
            if(line[1].code == PieceType.空 && line[2].code == PieceType.空 && line[3].code == friend &&
                    line[4].code == friend && line[5].code == friend && line[6].code == enemy){
                addInvalid(line[3].index,direction);
                addInvalid(line[5].index,direction);
                return type.get("001112");
            }
        }
        //##211100#
        if(line[2] != null && line[7] != null){
            if(line[2].code == enemy && line[3].code == friend && line[4].code == friend &&
                    line[5].code == friend && line[6].code == PieceType.空 && line[7].code == PieceType.空){
                addInvalid(line[3].index,direction);
                addInvalid(line[5].index,direction);
                return type.get("001112");
            }
        }
        //#00111###
        if(line[1] != null && line[5] != null){
            if(line[1].code == PieceType.空 && line[2].code == PieceType.空 && line[3].code == friend &&
                    line[4].code == friend && line[5].code == friend && line[6] == null){
                addInvalid(line[3].index,direction);
                addInvalid(line[5].index,direction);
                return type.get("001112");
            }
        }
        //###11100#
        if(line[3] != null && line[7] != null){
            if(line[2] == null && line[3].code == friend && line[4].code == friend &&
                    line[5].code == friend && line[6].code == PieceType.空 && line[7].code == PieceType.空){
                addInvalid(line[3].index,direction);
                addInvalid(line[5].index,direction);
                return type.get("001112");
            }
        }
        return 0;
    }
    private int isLive4(TestPiece[] line, PieceType friend, PieceType enemy, int direction){
        //##011110#
        if(line[2] != null && line[7] != null){
           if(line[2].code == PieceType.空 && line[3].code == friend &&
                   line[4].code == friend && line[5].code == friend && line[6].code == friend &&
                   line[7].code == PieceType.空){
               addInvalid(line[3].index,direction);
               addInvalid(line[5].index,direction);
               addInvalid(line[6].index,direction);
               return type.get("011110");
           }
        }
        //#011110##
        if(line[1] != null && line[6] !=null){
            if(line[1].code == PieceType.空 && line[2].code == friend && line[3].code == friend &&
                    line[4].code == friend && line[5].code == friend && line[6].code == PieceType.空){
                addInvalid(line[2].index,direction);
                addInvalid(line[3].index,direction);
                addInvalid(line[5].index,direction);
                return type.get("011110");
            }
        }
        return 0;
    }
    private int isJump4(TestPiece[] line, PieceType friend, PieceType enemy, int direction){
        //##011101#
        if(line[2] != null && line[7] != null){
            if(line[2].code == PieceType.空 && line[3].code == friend &&
                    line[4].code == friend && line[5].code == friend && line[6].code == PieceType.空 &&
                    line[7].code == friend){
                addInvalid(line[3].index,direction);
                addInvalid(line[5].index,direction);
                addInvalid(line[7].index,direction);
                return type.get("011101");
            }
        }
        //#101110##
        if(line[1] != null && line[6] != null){
            if(line[1].code == friend && line[2].code == PieceType.空 && line[3].code == friend &&
                    line[4].code == friend && line[5].code == friend && line[6].code == PieceType.空){
                addInvalid(line[1].index,direction);
                addInvalid(line[3].index,direction);
                addInvalid(line[5].index,direction);
                return type.get("011101");
            }
        }
        //###11011#
        if(line[3] != null && line[7] != null){
            if(line[3].code == friend && line[4].code == friend && line[5].code == PieceType.空 &&
                    line[6].code == friend && line[7].code == friend){
                addInvalid(line[3].index,direction);
                addInvalid(line[6].index,direction);
                addInvalid(line[7].index,direction);
                return type.get("11011");
            }
        }
        //#11011###
        if(line[1] != null && line[5] != null){
            if(line[1].code == friend && line[2].code == friend && line[3].code == PieceType.空 &&
                    line[4].code == friend && line[5].code == friend){
                addInvalid(line[1].index,direction);
                addInvalid(line[2].index,direction);
                addInvalid(line[5].index,direction);
                return type.get("11011");
            }
        }
        return 0;
    }
    private int isDieJump4(TestPiece[] line, PieceType friend, PieceType enemy, int direction){
        //##211101#
        if(line[2] != null && line[7] != null){
            if(line[2].code == enemy && line[3].code == friend &&
                    line[4].code == friend && line[5].code == friend && line[6].code == PieceType.空 &&
                    line[7].code == friend){
                addInvalid(line[3].index,direction);
                addInvalid(line[5].index,direction);
                addInvalid(line[7].index,direction);
                return type.get("211101");
            }
        }
        //#101112##
        if(line[1] != null && line[6] != null){
            if(line[1].code == friend && line[2].code == PieceType.空 && line[3].code == friend &&
                    line[4].code == friend && line[5].code == friend && line[6].code == enemy){
                addInvalid(line[1].index,direction);
                addInvalid(line[3].index,direction);
                addInvalid(line[5].index,direction);
                return type.get("211101");
            }
        }
        //###11101#
        if(line[3] != null && line[7] != null){
            if(line[2] == null && line[3].code == friend &&
                    line[4].code == friend && line[5].code == friend && line[6].code == PieceType.空 &&
                    line[7].code == friend){
                addInvalid(line[3].index,direction);
                addInvalid(line[5].index,direction);
                addInvalid(line[7].index,direction);
                return type.get("211101");
            }
        }
        //#10111###
        if(line[1] != null && line[5] != null){
            if(line[1].code == friend && line[2].code == PieceType.空 && line[3].code == friend &&
                    line[4].code == friend && line[5].code == friend && line[6] == null){
                addInvalid(line[1].index,direction);
                addInvalid(line[3].index,direction);
                addInvalid(line[5].index,direction);
                return type.get("211101");
            }
        }
        return 0;
    }
    private int isDie4(TestPiece[] line, PieceType friend, PieceType enemy, int direction){
        //##011112#
        if(line[2] != null && line[7] != null){
            if(line[2].code == PieceType.空 && line[3].code == friend && line[4].code == friend &&
                    line[5].code == friend && line[6].code == friend && line[7].code == enemy){
                addInvalid(line[3].index,direction);
                addInvalid(line[5].index,direction);
                addInvalid(line[6].index,direction);
                return type.get("011112");
            }
        }
        //#211110##
        if(line[1] != null && line[6] != null){
            if(line[1].code == enemy && line[2].code == friend && line[3].code == friend &&
                    line[4].code == friend && line[5].code == friend && line[6].code == PieceType.空){
                addInvalid(line[2].index,direction);
                addInvalid(line[3].index,direction);
                addInvalid(line[5].index,direction);
                return type.get("011112");
            }
        }
        //##01111##
        if(line[2] != null && line[6] != null){
            if(line[2].code == PieceType.空 && line[3].code == friend && line[4].code == friend &&
                    line[5].code == friend && line[6].code == friend && line[7] == null){
                addInvalid(line[3].index,direction);
                addInvalid(line[5].index,direction);
                addInvalid(line[6].index,direction);
                return type.get("011112");
            }
            //##11110##
            if(line[1] == null && line[2].code == friend && line[3].code == friend &&
                    line[4].code == friend && line[5].code == friend && line[6].code == PieceType.空){
                addInvalid(line[2].index,direction);
                addInvalid(line[3].index,direction);
                addInvalid(line[5].index,direction);
                return type.get("011112");
            }
        }
        return 0;
    }
    private int is5(TestPiece[] line, PieceType friend, PieceType enemy, int direction){
        //##11111##
        if(line[2] != null && line[6] != null){
            if(line[2].code == friend && line[3].code == friend && line[4].code == friend
                    && line[5].code == friend && line[6].code == friend){
                return type.get("11111");
            }
        }
        return 0;
    }
}
